﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CustomerInfoForm.aspx.cs"
    Inherits="PSS.WebSite.SetManage.BasicData.CustomerInfoForm" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>添加/编辑客户管理</title>
    <script src="../../lib/boot.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" method="post">
    <input name="CustomerId" class="mini-hidden" />
    <fieldset style="border: solid 1px #aaa; padding: 5px;">
        <legend>客户信息</legend>
        <table align="left" style="table-layout: fixed; width: 320;">
            <tr>
                <td style="width: 80px;" align="right">
                    客户编号：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="CustomerCode" class="mini-textbox" style="width: 300px;" required="true" />
                </td>
                <td style="width: 80px;" align="right">
                    客户名称：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="CustomerName" class="mini-textbox" style="width: 300px;" required="true" />
                </td>
            </tr>
            <tr>
                <td style="width: 80px;" align="right">
                    客户类别：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="CCategoryId" class="mini-combobox" style="width: 300px;" valuefield="CCategoryId"
                        textfield="CCategoryName" />
                </td>
                <td style="width: 80px;" align="right">
                    余额日期：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="BalanceDate" style="width: 300px;" class="mini-datepicker" required="true"
                        format="yyyy-MM-dd" />
                </td>
            </tr>
            <tr>
                <td style="width: 80px;" align="right">
                    期初应收款：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="InitialRec" id="InitialRec" class="mini-spinner" style="width: 300px;"
                        minvalue="0" maxvalue="999999999" decimalplaces="4" minheight="100" />
                </td>
                <td style="width: 80px;" align="right">
                    期初预收款：
                </td>
                <td style="width: 300px;" align="left">
                    <input name="InitialDeposit" id="InitialDeposit" class="mini-spinner" style="width: 300px;"
                        minvalue="0" maxvalue="999999999" decimalplaces="4" minheight="100" />
                </td>
            </tr>
        </table>
    </fieldset>
    <fieldset style="border: solid 1px #aaa; padding: 3px; height: 300px;">
        <legend>联系人信息</legend>
        <div class="mini-toolbar" style="padding: 2px; border-bottom: 0;">
            <table style="width: 100%;">
                <tr>
                    <td style="width: 100%;">
                        <a class="mini-button" iconcls="icon-add" onclick="addRow();" plain="true">添加</a>
                        <a class="mini-button" iconcls="icon-remove" onclick="removeRow()" plain="true">删除</a>
                    </td>
                </tr>
            </table>
        </div>
        <!--撑满页面-->
        <div class="mini-fit">
            <div id="datagrid1" class="mini-datagrid" style="width: 100%; height: 100%;" idfield="CContactId"
                showfooter="false" multiselect="true" allowresize="true" allowcelledit="true"
                allowcellselect="true">
                <div property="columns">
                    <div type="checkcolumn" width="6%">
                    </div>
                    <div field="Linkman" width="100" headeralign="center" allowsort="false">
                        联系人
                        <input property="editor" class="mini-textbox" style="width: 100%;" />
                    </div>
                    <div field="MobilePhone" width="100" headeralign="center" allowsort="false">
                        手机
                        <input property="editor" class="mini-textbox" style="width: 100%;" />
                    </div>
                    <div field="SpecialPlane" width="100" headeralign="center" allowsort="false">
                        座机
                        <input property="editor" class="mini-textbox" style="width: 100%;" />
                    </div>
                    <div field="QqMsn" width="80" headeralign="center" allowsort="false">
                        QQ/MSN
                        <input property="editor" class="mini-textbox" style="width: 100%;" />
                    </div>
                    <div field="DeliveryAddress" width="120" headeralign="center" allowsort="false">
                        送货地址<input property="editor" class="mini-textarea" style="width: 100%;" minheight="50" />
                    </div>
                    <div type="comboboxcolumn" autoshowpopup="true" renderer="onFlagRenderer" field="Flag" width="80" headeralign="center"
                        allowsort="false">
                        首要联系人<input property="editor" class="mini-combobox" style="width: 100%;" data="FlagData" />
                    </div>
                </div>
            </div>
        </div>
    </fieldset>
    <table style="width: 100%;">
        <tr>
            <td style="width: 12%;" align="right">
                备注：
            </td>
            <td style="width: 88%" align="left">
                <input name="Remark" class="mini-textarea" style="width: 100%; height: 50px;" />
            </td>
        </tr>
    </table>
    <div style="text-align: center; padding: 10px;">
        <a class="mini-button" iconcls="icon-save" onclick="onOk" style="width: 60px; margin-right: 20px;">
            保存</a> <a class="mini-button" iconcls="icon-cancel" onclick="onCancel" style="width: 60px;">
                取消</a>
    </div>
    </form>
    <script type="text/javascript">
        var FlagData = [{ id: 1, text: '是' }, { id: 0, text: '否'}];
        mini.parse();
        mini.getbyName("BalanceDate").setValue(new Date());
        var CCategoryId = mini.getbyName("CCategoryId");
        CCategoryId.setUrl("../../Service/SetManageHandler.ashx?action=GetCustomerCategoryListByCombobox");
        var action;
        var grid = mini.get("datagrid1");
        var form = new mini.Form("form1");
        function addRow() {
            var newRow = { name: "New Row" };
            grid.addRow(newRow, 0);
        }
        function removeRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                grid.removeRows(rows, true);
            }
        }
        function SaveData() {
            var arr = [];
            var data = grid.getChanges();
            for (var i = 0; i < data.length; i++) {
                if (data[i].Linkman != undefined) {
                    arr.push(data[i]);
                }
            }
            var o = form.getData();
            form.validate();
            if (form.isValid() == false) return;
            saveData(o, arr);
        }
        function saveData(o, data) {
            o.arrList = data;
            var json = mini.encode([o]);
            if (viewaction == "edit") {
                $.ajax({
                    url: "../../Service/SetManageHandler.ashx?action=ModifyCustomer",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
            else {
                $.ajax({
                    url: "../../Service/SetManageHandler.ashx?action=AddCustomer",
                    data: { data: json },
                    type: 'POST',
                    success: function (text) {
                        CloseWindow("save");
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });
            }
        }

        function SetData(data) {
            viewaction = data.action;
            if (data.action == "edit") {
                data = mini.clone(data);
                $.ajax({
                    url: "../../service/SetManageHandler.ashx?action=GetCustomerInfo&CustomerId=" + data.CustomerId,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        $.ajax({
                            url: "../../service/SetManageHandler.ashx?action=GetCustomerContactsList&CustomerId=" + data.CustomerId,
                            cache: false,
                            success: function (text) {
                                var o = mini.decode(text);
                                grid.setData(o);
                            },
                            error: function (jqXHR, textStatus, errorThrown) {
                                alert(jqXHR.responseText);
                            }
                        });
                    },
                    error: function (jqXHR, textStatus, errorThrown) {
                        alert(jqXHR.responseText);
                    }
                });

            }
        }
        function onFlagRenderer(e) {
            for (var i = 0, l = FlagData.length; i < l; i++) {
                var g = FlagData[i];
                if (g.id == e.value) return g.text;
            }
            return "";
        }
        function GetData() {
            var o = form.getData();
            return o;
        }
        function CloseWindow(action) {
            if (action == "close" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                    return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();
        }

        function onOk() {
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
    </script>
</body>
</html>
